<?php

class ClientController extends MyClass_Action {
	protected $controller;
	protected $action;
	protected $_user;
	
	public function init() {
		
		$this->commonAction();	
	
	}
	
	public function indexAction(){
		try {
			//取数据
			$sql="SELECT * from clients ORDER BY groupid,schoolid asc";
			//echo $sql; echo '<br />';
			$result=$this->_dbAdapter ->fetchAll ( $sql );
			
			$groupidlist=array();
			$schoolidlist=array();
			$outdatetime=time()-900;
			$groupid_schoolid=array();
			
			
			foreach($result as $key => $value){
				if(!in_array($value['groupid'],$groupidlist))
					$groupidlist[]=$value['groupid'];
					
				if(!in_array($value['schoolid'],$schoolidlist))
					$schoolidlist[]=$value['schoolid'];
					
				if(!in_array($value['schoolid'],$groupid_schoolid[$value['groupid']])){
					$groupid_schoolid[$value['groupid']][]=$value['schoolid'];
					$result[$key]['isold']=0;
				}else
					$result[$key]['isold']=1;
					
				if(strtotime($value['lasttime'])>$outdatetime)
					$result[$key]['isouttime']=0;
				else
					$result[$key]['isouttime']=1;
				
				 $result[$key]['lasttimeDisp']=MyClass_Common::getDateInfo(strtotime($value['lasttime']));
			}
			$this->view->groupnamelist=$this->_dbAdapter ->fetchPairs ('select groupid,groupname from groups where groupid in ('.implode(',',$groupidlist).')');
			$this->view->schoolnamelist=$this->_dbAdapter ->fetchPairs ('select schoolid,schoolname from class where schoolid in ('.implode(',',$schoolidlist).')');
			$this->view->result=$result;

			//print_r($this->view->schoolnamelist);
			// print_r($result);
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	public function listAction(){
		try {

			//取数据
			$sql="SELECT max(clientid) as clientid from clients group by schoolid,groupid";
			$clientidlist=$this->_dbAdapter ->fetchCol ( $sql );
			//取数据
			$sql="SELECT * from clients where clientid in (".implode(',',$clientidlist).") ORDER BY groupid,schoolid asc";
			//echo $sql;
			$result=$this->_dbAdapter ->fetchAssoc ( $sql );
			
			$groupidlist=array();
			$schoolidlist=array();
			$outdatetime=time()-900;
			$groupid_schoolid=array();
			
			$sum=array('all_num'=>0,'noup_thumb_num'=>0,'noup_source_num'=>0,'isdelete_num'=>0,'today'=>date('Y-m-d'),'today_num'=>0,'count_num'=>count($result),'count_today_num'=>0);
			foreach($result as $key => $value){
				if(!in_array($value['groupid'],$groupidlist))
					$groupidlist[]=$value['groupid'];
					
				if(!in_array($value['schoolid'],$schoolidlist))
					$schoolidlist[]=$value['schoolid'];
					
				if(!in_array($value['schoolid'],$groupid_schoolid[$value['groupid']])){
					$groupid_schoolid[$value['groupid']][]=$value['schoolid'];
					$result[$key]['isold']=0;
				}else
					$result[$key]['isold']=1;
					
				if(strtotime($value['lasttime'])>$outdatetime)
					$result[$key]['isouttime']=0;
				else
					$result[$key]['isouttime']=1;
				
				 $result[$key]['lasttimeDisp']=MyClass_Common::getDateInfo(strtotime($value['lasttime']));
				 
				 $sum['all_num'] +=$value['all_num'];
				 $sum['noup_thumb_num'] +=$value['noup_thumb_num'];
				 $sum['noup_source_num'] +=$value['noup_source_num'];
				 $sum['isdelete_num'] +=$value['isdelete_num'];
				 
				 if(date('Y-m-d')==$value['today']){
				 	$sum['today_num'] +=$value['today_num'];
					if($value['today_num']>0)
						$sum['count_today_num']++;
				 }
				 
			}
			$this->view->groupnamelist=$this->_dbAdapter ->fetchPairs ('select groupid,groupname from groups where groupid in ('.implode(',',$groupidlist).')');
			$this->view->schoolnamelist=$this->_dbAdapter ->fetchPairs ('select schoolid,schoolname from class where schoolid in ('.implode(',',$schoolidlist).')');
			$this->view->result=$result;
			$this->view->sum=$sum;

			//print_r($this->view->schoolnamelist);
			//print_r($result);exit;
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}

	
	
	
	public function deleteAction(){
		try {
				
			$clientid = ( int ) $this->_request->getParam ( 'clientid' );

			if (! $clientid)
				throw new Exception ( '参数错误！' );
			
			$result=$this->_dbAdapter->delete('clients','clientid='.$clientid);
		
			if($result){
				$this->feedback ( 'success', 'success', '/'.$this->controller.'/index' , 'tip', 0 );
			}else
				throw new Exception ( '没有此项或已删除，请刷新看看！' );
			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}

	
	public function clearAction(){
		try {
			//取数据
			$sql="SELECT max(clientid) as clientid from clients group by schoolid,groupid";
			$clientidlist=$this->_dbAdapter ->fetchCol ( $sql );
			
			$sql='delete from clients where clientid not in ("'.implode('","',$clientidlist).'")';
			$result=$this->_dbAdapter->query($sql);
			$this->feedback ( 'success', 'success', '/'.$this->controller.'/index' , 'tip', 0);
			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	

	 //以下内容保持不变
	public function noRouteAction() {
		$this->_redirect ( '/' );
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		throw new Exception ( 'Invalid method' );
	}}

